﻿using System;
using System.Collections;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace JWBSTAPP.CommonClass
{

    public class ResolveColumn
    {
        public string oldColumnName { get; set; }//旧的列名
        public string newColumnName { get; set; }//新的列名
        public Type newType { get; set; }//新列的数据类型
        public Hashtable columnData { get; set; }//旧列数据和新列数据的对应关系，相当于字典对照表
    }

    public class ResolveReference
    {
        public static DataTable ResolveDataTable(DataTable oldTable, ResolveColumn[] resolveColumnArray)
        {

            foreach (ResolveColumn resolveColumn in resolveColumnArray)
            {
                foreach (DataColumn column in oldTable.Columns)
                {
                    if (column.ColumnName == resolveColumn.oldColumnName)
                    {
                        oldTable.Columns[column.ColumnName].DataType = resolveColumn.newType;
                        if (!String.IsNullOrEmpty(resolveColumn.newColumnName))
                            oldTable.Columns[column.ColumnName].ColumnName = resolveColumn.newColumnName;
                        break;
                    }
                }
            }

            foreach (ResolveColumn resolveColumn in resolveColumnArray)
            {
                for (int i = 0; i < oldTable.Rows.Count; i++)
                {
                    if (!String.IsNullOrEmpty(resolveColumn.newColumnName))
                        oldTable.Rows[i][resolveColumn.newColumnName] = resolveColumn.columnData[oldTable.Rows[i][resolveColumn.newColumnName]];
                    else
                        oldTable.Rows[i][resolveColumn.oldColumnName] = resolveColumn.columnData[oldTable.Rows[i][resolveColumn.oldColumnName]];
                }
            }

            return oldTable;
        }
    }
}